package com.jianbo.proxy.util;

import java.io.InputStream;
import java.net.URL;

public class ResourceUtil {

    public static byte[] read(String path) {
        try {
            URL url = getResource(path);
            InputStream in = url.openStream();
            return IoUtil.read(in);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage() + path, e);
        }
    }

    public static String readString(String path) {
        if(path.startsWith("/")) {
            throw new RuntimeException("startsWith / is error");
        }
        byte[] bytes = read(path);
        return ByteUtil.toStr(bytes);
    }

    public static URL getResource(String path) {
        ClassLoader classLoader = ResourceUtil.class.getClassLoader();
        URL url = classLoader.getResource(path);
        return url;
    }

    
}
